<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/18 0018
 * Time: 上午 9:58
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Category extends BasicAdmin
{
    public $table = 'CategoryModule';

    /**
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function about(){
        $this->title = '分栏';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at,type')->where(['is_deleted'=>1,'pid'=>1])->order('sort asc,id asc');
        return parent::_list($db,false,true,true,[],'cate');
    }

    /**业务体系
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function business(){
        $this->title = '分栏';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at,type')->where(['is_deleted'=>1,'pid'=>3])->order('sort asc,id asc');
        return parent::_list($db,false,true,true,[],'cate');
    }

    /**师资培训
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function teacher(){
        $this->title = '分栏';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at,type')->where(['is_deleted'=>1,'pid'=>13])->order('sort asc,id asc');
        return parent::_list($db,false,true,true,[],'cate');
    }

    /**招商加盟
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function join(){
        $this->title = '分栏';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at,type')->where(['is_deleted'=>1,'pid'=>15])->order('sort asc,id asc');
        return parent::_list($db,false,true,true,[],'cate');
    }

    public function experts(){
        $this->title = '分栏';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at,type')->where(['is_deleted'=>1,'pid'=>16])->order('sort asc,id asc');
        return parent::_list($db,false,true,true,[],'cate');
    }





    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $id = $this->request->post('id');
        $id_arr = explode(',', $id);
        foreach ($id_arr as $i) {
            if (in_array($i, [1, 2, 3, 31, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 32, 24, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 39])) {
                $this->error('请勿删除默认模块，不需要时请禁用');
            }
        }
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}